<template>
	<view class="page">
		<view class="bar_info" :style="{height:(88+StatusBarRpx)+'rpx',paddingTop:StatusBarRpx+'rpx'}">
			<view class="title_bar">
				<view class="title_bar_left" @tap.stop="handleJump" data-type="BACK">
					<image class="title_bar_left_icon" :src='STATIC_URL+"5.png"'></image>
				</view>
				<view class="title_bar_center">
					库存管理
				</view>
				<view class="title_bar_right"></view>
			</view>
		</view>
		<view :style="{height: (88+StatusBarRpx)+'rpx'}"></view>
		<u-sticky bg-color="#f5f5f5" :offset-top="88+StatusBarRpx" style="width: 100%;">
			<view class="flex align-center justify-between" style="padding: 10rpx 25rpx;background: #f5f5f5;width: 100%;">
				<view class="search_info">
					<image class="search_icon" :src='STATIC_URL+"9.png"' mode="aspectFill"></image>
					<input v-model="keyword" class="search_input" type="text" placeholder="输入商品关键词" placeholder-style="font-size: 26rpx;color: #8B8B8B;" @confirm="getSearch">
				</view>
				<view class="search_text" @tap.stop="getSearch">搜索</view>
			</view>
		</u-sticky>
		<view class="loading_css" v-if="isShow">
			<u-loading size="36" color="#41AF36"></u-loading>
			<view class="loading_text">加载中</view>
		</view>
		<template v-else>
			<view class="shop_list">
				<view class="shop_item flex align-center" v-for="(item, index) in list" :key="index">
					<view class="select_view flex">
						<view class="item_left_img">
							<u-lazy-load :image="item.image" img-mode="aspectFill" height="140" border-radius="8"></u-lazy-load>
						</view>
						<view class="item_right">
							<view class="shop_name flex align-center">
								<view class="shop_name_text">{{item.product_name}}</view>
							</view>
							<view class="specification_text">
								<text>规格：</text>
								<text>{{item.specification}}</text>
							</view>
							<view class="operate flex align-center justify-between">
								<view class="shop_infomation">
									<view class="infomation_item">
										<text>库存数量：</text>
										<text>{{item.remain_num}}{{filterDictionary(item.unit)}}</text>
									</view>
									<view class="infomation_item">
										<text>过期日期：</text>
										<text>{{item.expire_date}}</text>
									</view>
								</view>
								<view class="operate_btn_1 flex align-center justify-center" @tap.stop="openCount(item)">操作</view>
							</view>
						</view>
					</view>
				</view>
				<u-loadmore v-if="last_page > 1" class="loadmore" font-size="24" :status="status"
					:icon-type="iconType" :load-text="loadText" />
			</view>
			<view class="noData" v-if="list.length == 0 && !isShow">
				<image src="/static/empty/no.png"></image>
			</view>
		</template>
		
		<u-mask :show="show" @click="show = false">
			<view class="warp">
				<view class="rect" @tap.stop>
					<view class="rect_1">使用数量</view>
					<view class="rect_2">
						<input type="text" v-model="amount_use_count" placeholder="请输入使用数量" placeholder-style="font-size: 24rpx;color: #3d3d3d;">
					</view>
					<view class="rect_3 flex align-center justify-between">
						<view class="rect_3_1 flex align-center justify-center" @tap="show = false">取消</view>
						<view class="rect_3_2 flex align-center justify-center" @tap="confirmReduce">确定</view>
					</view>
				</view>
			</view>
		</u-mask>
	</view>
</template>

<script>
	import {
		validate
	} from '@/common/utils/validate.js'
	export default {
		data() {
			return {
				image: '/static/index/3.png',
				body: "",
				isShow: true,
				is_manage: false,
				is_total: false,
				product_id: [],
				list: [],
				"page": 1,
				"is_pull": true,
				"last_page": 1,
				"status": 'loadmore',
				"iconType": 'flower',
				"loadText": {
					loadmore: '轻轻上拉',
					loading: '努力加载中',
					nomore: '我也是有底线的'
				},
				user: {},
				show: false,
				keyword: "",
				amount_use_count: 0,
				original_num: 0,
				shop_index: 0,
				order_id: "",
			}
		},
		watch: {
			amount_use_count() {
				if (this.amount_use_count > Number(this.original_num)) {
					this.amount_use_count = Number(this.original_num);
				}
			}
		},
		onLoad() {
			this.getData();
		},
		onShow() {
			this.user = global.userInfo;
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.page = 1;
			this.is_pull = true;
			this.isShow = true;
			this.getData();
		},
		// 上拉加载
		onReachBottom() {
			if (this.page >= this.last_page) return;
			this.status = 'loading';
			this.page++;
			setTimeout(() => {
		
				if (this.page >= this.last_page) this.status = 'nomore';
				else this.status = 'loading';
				this.is_pull = false;
				this.getData();
			}, 500)
		},
		methods: {
			getData() {
				this.$api.get(global.apiUrls.gylInventory, {
					product_name: this.keyword,
					page: this.page,
					pageSize: 10
				}).then(res => {
					// console.log(res);
					uni.stopPullDownRefresh();
					this.isShow = false;
					if (res.data.code == 200) {
						this.last_page = res.data.data.pageInfo.totalPage;
						if (this.is_pull) {
							this.list = res.data.data.items;
						} else {
							this.list = this.list.concat(res.data.data.items);
						}
					} else {
						this.$message.info(res.data.message);
					}
				})
			},
			getSearch() {
				this.page = 1;
				this.is_pull = true;
				this.isShow = true;
				this.getData();
			},
			// 操作按钮打开弹窗
			openCount(item, index) {
				this.show = true;
				this.original_num = item.remain_num;
				this.amount_use_count = item.remain_num;
				this.order_id = item.id;
				this.shop_index = index;
			},
			confirmReduce() {
				if (isNaN(Number(this.amount_use_count))) {
					this.$message.info('请输入正确的使用数量');
					return;
				}
				if (this.amount_use_count <= 0) {
					this.$message.info('使用数量必须大于0');
					return;
				}
				this.show = false;
				this.$api.post(global.apiUrls.gylOutWarehouse, {
					order_detail_id: this.order_id,
					out_num: this.amount_use_count
				}).then(res => {
					this.$message.info(res.data.message);
					if (res.data.code == 200) {
						this.page = 1;
						this.is_pull = true;
						this.isShow = true;
						this.getData();
					}
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	.page {
		background: #f5f5f5;
		background-size: 100% auto !important;
		.bar_info {
			width: 750rpx;
			height: 88rpx;
			overflow: hidden;
			z-index: 10;
			position: fixed;
			top: 0rpx;
		
			.title_bar {
				padding: 0 32rpx;
				display: flex;
				justify-content: space-between;
				align-items: center;
		
				.title_bar_left {
					width: 50rpx;
					.title_bar_left_icon {
						width: 18rpx;
						height: 32rpx;
					}
				}
		
				.title_bar_center {
					line-height: 88rpx;
					font-size: 36rpx;
					font-weight: 400;
				}
		
				.title_bar_right {
					width: 50rpx;
				}
			}
		}
		
	}
	.btn_info {
		width: 100%;
		height: 110rpx;
		position: fixed;
		bottom: 0;
		padding: 15rpx 25rpx;
		z-index: 10;
		.save_btn {
			width: 700rpx;
			height: 80rpx;
			background: #41AF36;
			border-radius: 50rpx;
			font-size: 30rpx;
			font-weight: 500;
			color: #fff;
			text-align: center;
			line-height: 80rpx;
			
		}
	}
	.operate_btn {
		width: 100%;
		height: 110rpx;
		position: fixed;
		bottom: 0;
		padding: 15rpx 25rpx;
		z-index: 10;
		background: #fff;
		.operate_1 {
			font-weight: 400;
			font-size: 26rpx;
			color: #A9A9A9;
			image {
				width: 40rpx;
				height: 40rpx;
				margin-right: 20rpx;
			}
		}
		.operate_2 {
			width: 214rpx;
			height: 80rpx;
			background: #41AF36;
			border-radius: 50rpx;
			font-weight: 500;
			font-size: 30rpx;
			color: #FFFFFF;
		}
	}
	.shop_list {
		padding: 20rpx 20rpx 0;
		.shop_item {
			background: #fff;
			border-radius: 10rpx;
			padding: 30rpx 20rpx;
			margin-bottom: 20rpx;
			.select_view {
				width: 100%;
				.item_left_img {
					width: 140rpx;
					height: 140rpx;
				}
				/deep/.u-lazy-item {
					width: 140rpx !important;
					height: 140rpx !important;
				}
				.item_right {
					width: calc(100% - 140rpx);
					padding: 0 0 0 30rpx;
					.shop_name {
						.shop_name_text {
							width: calc(100% - 40rpx);
							font-weight: 500;
							font-size: 32rpx;
							color: #3D3D3D;
						}
					}
					.specification_text {
						font-size: 24rpx;
						color: rgba(61,61,61,0.5);
						margin-top: 8rpx;
					}
					.operate {
						margin-top: 8rpx;
					}
					.shop_infomation {
						.infomation_item {
							font-size: 24rpx;
							color: rgba(61,61,61,0.5);
						}
					}
					.operate_btn_1 {
						width: 130rpx;
						height: 50rpx;
						background: #41AF36;
						border-radius: 10rpx;
						font-size: 24rpx;
						color: #fff;
					}
				}
			}
		}
	}
	.search_info {
		// margin: 10rpx 25rpx 0;
		width: 610rpx;
		height: 70rpx;
		background: #fff;
		border-radius: 100rpx;
		position: relative;
		
		.search_icon {
			width: 30rpx;
			height: 30rpx;
			position: absolute;
			left: 30rpx;
			top: 20rpx;
		}
		.search_input {
			height: 70rpx;
			display: flex;
			flex: 1;
			padding: 0 32rpx 0 80rpx;
			font-size: 26rpx;
		}
	}
	.search_text {
		font-weight: 400;
		font-size: 30rpx;
		color: #41AF36;
	}
	
	
	.warp {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 100%;
	}
	
	.rect {
		width: 600rpx;
		height: 360rpx;
		background-color: #fff;
		background: url(https://www.shiyaocanyin.com/images/32.png) no-repeat;
		background-size: 100% 100%;
	}
	
	.rect_1 {
		font-weight: 500;
		font-size: 42rpx;
		color: #3D3D3D;
		text-align: center;
		padding: 40rpx 0 36rpx;
	}
	
	.rect_2 {
		font-weight: 400;
		font-size: 24rpx;
		color: #3D3D3D;
		text-align: left;
		margin: 0 30rpx;
		input {
			height: 70rpx;
			background: #F0F0F0;
			border-radius: 100rpx;
			padding: 0 30rpx;
			width: 100%;
		}
	}
	
	.rect_3 {
		margin-top: 56rpx;
		padding: 0 60rpx;
	
		.rect_3_1 {
			width: 220rpx;
			height: 70rpx;
			background: #F0F0F0;
			border-radius: 50rpx;
			font-weight: 500;
			font-size: 30rpx;
			color: #323232;
		}
	
		.rect_3_2 {
			width: 220rpx;
			height: 70rpx;
			background: #41AF36;
			border-radius: 50rpx;
			font-weight: 500;
			font-size: 30rpx;
			color: #fff;
		}
	}
</style>